package com.learn.spring.service;

import com.learn.spring.entity.FluxUser;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;

@Service
public class FluxService implements IFluxService {

    // 模拟数据库操作
    private final Map<Integer, FluxUser> userMap = new HashMap<>();

    public FluxService() {
        this.userMap.put(1, new FluxUser("summer", "man", 33));
        this.userMap.put(2, new FluxUser("lw", "woman", 20));
    }


    @Override
    public Mono<FluxUser> getUserById(int id) {
        return Mono.justOrEmpty(this.userMap.get(id));
    }

    @Override
    public Flux<FluxUser> getAllUser() {
        return Flux.fromIterable(userMap.values());
    }

    @Override
    public Mono<Void> addUser(Mono<FluxUser> userMono) {
        return userMono.doOnNext(user -> {
            this.userMap.put(this.userMap.size() + 1, user);
        }).then(Mono.empty());
    }

}
